
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  importPool;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.RandomAccessFile;
import java.util.Stack;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import logger.PoolLogger;


import dbmanager.DBManager;
import dbmanager.GetIdName;

import utility.CapitalChar;


public class SelectFields  extends JFrame implements  ItemListener,ActionListener
{
	
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private Vector<Object>jcheck = new Vector<Object>();
	private Vector<Object>jSelectedcheck = new Vector<Object>();
	private String[] arrFiels;
	private JButton Register;
	private JCheckBox jc;
	private String inputpath=" ";	
	private String tableName=" ";
	
	GetIdName gin=new GetIdName();
	
	private Stack< Object>importStack=new Stack<Object>();
	PoolLogger pl;
	
	public SelectFields(String[] arrFields, String path, String tablename,Stack<Object>importStack)
	{
		super(); 
 		this.arrFiels=arrFields;
 		this.importStack=importStack;
 		System.out.println("importStack push ==========="+importStack);
 		this.inputpath=path;
 		this.tableName=tablename;
 		
 		initializeComponent(arrFiels);
 		
 		this.setVisible(true);
 		
 		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	
	private void initializeComponent(String[] arrFields ) 
 	{ 
		System.out.print("i am in initi");
		pl.info("I am in initi");
		pl.debug("I am in initi");
		pl.warn("I am in initi");
		pl.fatal("I am in initi");
		pl.error("I am in initi");
		pl.trace("I am in initi");
       	String fieldStr="";
       	contentPane = (JPanel)this.getContentPane();
       	contentPane.setLayout(null);
    	Register = new JButton();
    	Register.setText("Register");
    	
    	Register.addActionListener(this);
    	int y=0;
    	int i=0;
    	
    	int fieldLength = arrFields.length;
    	
		for( y=0;y<fieldLength; )
 		{
			System.out.println("length===="+fieldLength);
			pl.info("length===="+fieldLength);
			pl.debug("length===="+fieldLength);
			pl.warn("length===="+fieldLength);
			pl.fatal("length===="+fieldLength);
			pl.error("length===="+fieldLength);
			pl.trace("length===="+fieldLength);
    		int x=10;
			int count=3,k=y;
			System.out.println("count ="+count);
			
			System.out.println("arrFields ="+fieldLength);
			pl.info("arrFields ="+fieldLength);
			pl.debug("arrFields ="+fieldLength);
			pl.warn("arrFields ="+fieldLength);
			pl.fatal("arrFields ="+fieldLength);
			pl.error("arrFields ="+fieldLength);
			pl.trace("arrFields ="+fieldLength);
			
			while(count!=0)
			{
				System.out.println("count is"+count);
				pl.info("count is"+count);
				pl.debug("count is"+count);
				pl.warn("count is"+count);
				pl.fatal("count is"+count);
				pl.error("count is"+count);
				pl.trace("count is"+count);
				System.out.println("i is"+i);
				pl.info("i is"+i);
				pl.debug("i is"+i);
				pl.warn("i is"+i);
				pl.fatal("i is"+i);
				pl.error("i is"+i);
				pl.trace("i is"+i);
				try
				{
					fieldStr=arrFields[i];
				}
				catch (Exception e) 
				{
					break;
				}
				
				System.out.println("fieldStr''''''''''''''''''======="+fieldStr);
				pl.info("fieldStr''''''''''''''''''======="+fieldStr);
				pl.debug("fieldStr''''''''''''''''''======="+fieldStr);
				pl.warn("fieldStr''''''''''''''''''======="+fieldStr);
				pl.fatal("fieldStr''''''''''''''''''======="+fieldStr);
				pl.error("fieldStr''''''''''''''''''======="+fieldStr);
				pl.trace("fieldStr''''''''''''''''''======="+fieldStr);
				jc=new JCheckBox(fieldStr);
				contentPane.add(jc);
				//addComponent(contentPane,jc,x,(k)*20,170,30);//default
				
				addComponent(contentPane,jc,x,(k)*20,170,30);
				jc.addItemListener(this);
				jc.setVisible(true);
				jcheck.add(jc);
				count--;
				System.out.println("count"+count);
				pl.info("count"+count);
				pl.debug("count"+count);
				pl.warn("count"+count);
				pl.fatal("count"+count);
				pl.error("count"+count);
				pl.trace("count"+count);
				
				i++;
				x=x+200;
				y++;
				System.out.println("y '''''''''''''''''''''======="+y);
				pl.info("y '''''''''''''''''''''======="+y);
				pl.debug("y '''''''''''''''''''''======="+y);
				pl.warn("y '''''''''''''''''''''======="+y);
				pl.fatal("y '''''''''''''''''''''======="+y);
				pl.error("y '''''''''''''''''''''======="+y);
				pl.trace("y '''''''''''''''''''''======="+y);
				
				System.out.print("x="+x+"y="+y*20);
				pl.info("x="+x+"y="+y*20);
				pl.debug("x="+x+"y="+y*20);
				pl.warn("x="+x+"y="+y*20);
				pl.fatal("x="+x+"y="+y*20);
				pl.error("x="+x+"y="+y*20);
				pl.trace("x="+x+"y="+y*20);
			}
		}
		//addComponent(contentPane, Register,300,y*20+100,120,30);//default
		
		int dimR = 200;
		if(fieldLength<3)
			dimR = 35*fieldLength;
    	addComponent(contentPane, Register,dimR,y*18+100,120,30);
 		
   		this.setTitle("SelectFields"); 
 		this.setLocation(new Point(0, 0)); 
 		//this.setSize(new Dimension(457, 600));//default
 		
 		int dimX =  525;
 		int dimY =  150;
 		
 		if(fieldLength<3)
 			dimX = 200*(fieldLength);
 		
 		if(fieldLength>3)
 		{
 			int inY = fieldLength/3;
 			dimY = 140 * (inY+1);
 			if(inY>=3)
 				dimY = dimY - (50*inY);
 		} 		
 		
 		this.setSize(new Dimension(dimX,dimY));
 	} 
  
 	
    private void addComponent(Container container,Component c,int x,int y,int width,int height) 
 	{ 
 		c.setBounds(x,y,width,height); 
 		container.add(c); 
 	} 
    
    
  	@SuppressWarnings("unchecked")
	public void actionPerformed(ActionEvent ae)
	{	
		if(ae.getSource()==Register)
		{
			
			try
				{
				for(int	i=0;i<jcheck.size();i++)
				{
					jc=(JCheckBox) jcheck.get(i);
					if(jc.isEnabled()==true)
					{
						if(jc.isSelected()==true)
						{
							//JOptionPane.showMessageDialog(this, jc.getText());
							jSelectedcheck.add(jc.getText());
						}
					}
				}
				if(!jSelectedcheck.isEmpty())
				{
					
					RandomAccessFile raf = new RandomAccessFile(inputpath,"rws");						
					String line = null;
					line=raf.readLine();
					String[] column = line.split("\t");
					//first column must be the object
					if(jSelectedcheck.contains(column[0]))
					{							
						
						try
						{
							Class C = Class.forName("importPool."+CapitalChar.makeFirstCharCapital(tableName));
							TableName tbl_name = (TableName) C.newInstance();
							tbl_name.initialize(inputpath,jSelectedcheck);
							C = null; 
						}
						
						catch (ClassNotFoundException e) 
						{
							e.printStackTrace();
						}
						catch (InstantiationException e) 
						{
							e.printStackTrace();
						}
						catch (Exception e) 
						{
							
						} 
						setVisible(false);
						while (!importStack.empty())
						{
							//importStack.pop();
							try {
								ImportFile im = (ImportFile) importStack.pop();
								im.setVisible(true);
							} catch (Exception e) {
							
							}
						}
					}
					else
					{
						JOptionPane.showMessageDialog(this, "Object '"+column[0]+"' must be selected");
					}
				}
				else
				{
					JOptionPane.showMessageDialog(this, "Select atleast one option");
				}
					
				}
			catch(Exception e)
			{
				e.printStackTrace();
				JOptionPane.showMessageDialog(this, "Unable to get input path");
			}
		}
	}
 	
	public void itemStateChanged(ItemEvent event)
	{
	}
	
	
	/*public static void main(String[] args)
	{
		
		String []fields = {"Item","Discount","Unit","Buy","Sell"};//,"Discount","Unit","Buy","Sell"
		String tablename = "master_table";
		String path = "/home/wiki/Desktop/ItemList/Item.txt";
		Stack< Object>importStack=new Stack<Object>();
		SelectFields sf = new SelectFields(fields,path,tablename,importStack);
		
	}*/
		
}
